<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="1200"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{ top: '93px', left: '154px' }"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top: 10%; height: 80%"
    >

      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="customerModal">

            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
                <a-input placeholder="请输入名称" v-decorator.trim="['customerName', validatorRules.customerName]" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="编号">
                <a-input placeholder="请输入编号" v-decorator.trim="['customerNo', validatorRules.customerNo]" />
              </a-form-item>
            </a-col>
            
            
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="业务员">
                <a-select
                  placeholder="选择业务员"
                  v-decorator="['salespersonId']"
                  :dropdownMatchSelectWidth="false"
                >
                  <a-select-option v-for="(item, index) in salespersonist" :key="index" :value="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="收款条件">
                <a-select placeholder="选择收款条件" v-decorator="['paymentTerms']" :dropdownMatchSelectWidth="false">
                  <a-select-option v-for="(item, index) in paymentTerms" :key="index" :value="item">
                    {{ item }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人">
                <a-input placeholder="请输入联系人" v-decorator.trim="['contactPerson']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电话">
                <a-input placeholder="请输入电话" v-decorator.trim="['phone']" />
              </a-form-item>
            </a-col>
           
           
            
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="纳税人识别号">
                <a-input placeholder="请输入纳税人识别号" v-decorator.trim="['taxIdentificationNumber']" />
              </a-form-item>
            </a-col>
           
           
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="开户行">
                <a-input placeholder="请输入开户行" v-decorator.trim="['bankName']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="账号">
                <a-input placeholder="请输入账号" v-decorator.trim="['bankAccount']" />
              </a-form-item>
            </a-col>
            
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地区">
                <a-input placeholder="请输入地区" v-decorator.trim="['region']" />
              </a-form-item>
            </a-col>
           
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="省">
                <a-input placeholder="请输入省" v-decorator.trim="['province']" />
              </a-form-item>
            </a-col>
           
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="市">
                <a-input placeholder="请输入市" v-decorator.trim="['city']" />
              </a-form-item>
            </a-col>
           
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="区">
                <a-input placeholder="请输入区" v-decorator.trim="['district']" />
              </a-form-item>
            </a-col>
           
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
                <a-input placeholder="请输入地址" v-decorator.trim="['deliveryAddress']" />
              </a-form-item>
            </a-col>
           
           
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否启用">
                <a-switch default-checked @change="status = !status" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
                <a-textarea :rows="2" placeholder="请输入备注" v-decorator.trim="['remark']" />
              </a-form-item>
            </a-col>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import { addCustomer, editCustomer, checkSupplier, getPaymentTerms, userAll } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
import { mixinDevice } from '@/utils/mixin'
export default {
  name: 'CustomerModal',
  mixins: [mixinDevice],
  data() {
    return {
      salespersonist: '',
      status: true,
      paymentTerms: [],
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        customerName: {
          rules: [
            { required: true, message: '请输入名称!' },
            { min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur' },
          ],
        },
        customerNo: {
          rules: [{ required: true, message: '请输入名称!' }],
        },
      },
    }
  },
  created() {},
  mounted() {
    userAll().then((res) => {
      this.salespersonist = res.data
    })
    getPaymentTerms().then((res) => {
      this.paymentTerms = res.data
    })
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'id',
'customerCategoryId',
'priceFieldId',
'priceFieldName',
'customerCategoryName',
'customerNo',
'customerName',
'paymentTerms',
'region',
'salespersonName',
'salespersonId',
'contactPerson',
'taxIdentificationNumber',
'bankName',
'bankAccount',
'phone',
'qqWechat',
'receivableBalance',
'associatedRoute',
'associatedRouteId',
'province',
'city',
'district',
'deliveryAddress',
'remark',
'status',
          )
        )
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          formData.type = '客户'
          formData.status = this.status ? 1 : 0
          let obj
          if (!this.model.id) {
            obj = addCustomer(formData)
          } else {
            obj = editCustomer(formData)
          }
          obj
            .then((res) => {
              if (res.code === 200) {
                that.$emit('ok')
              } else {
                that.$message.warning(res.msg)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0,
      }
      checkSupplier(params).then((res) => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    },
  },
}
</script>
<style scoped>
</style>